package c926.xiaoduyun.xiaodu.mservice;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.control.AttachmentPanel;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;
import kd.sdk.plugin.Plugin;
import org.apache.commons.lang.StringUtils;

import java.util.Collections;
import java.util.EventObject;
import java.util.List;
import java.util.Map;

/**
 * 人员申请单界面插件
 */
public class XiaoduApplyListPlugin extends AbstractBillPlugIn implements Plugin {

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        // 查询并返回用户信息
        getUserInfoByUserId();
    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        FormOperate operation = (FormOperate) args.getSource();
        String key = operation.getOperateKey();
        if (StringUtils.equals("save", key) || StringUtils.equals("submit", key) || StringUtils.equals("submitandnew", key)) {
            AttachmentPanel attachment = this.getControl("attachmentpanel");
            List<Map<String, Object>> attachmentData = attachment.getAttachmentData();
            if (CollectionUtils.isEmpty(attachmentData)) {
                this.getView().showTipNotification("附件不能为空，需上传身份证照片！");
                args.setCancel(Boolean.TRUE);
            }
        }
    }


    private void getUserInfoByUserId() {
        long currentUserId = UserServiceHelper.getCurrentUserId();
        long orgId = UserServiceHelper.getUserMainOrgId(currentUserId);
        Map<Long, Long> company = UserServiceHelper.getCompanyByUserIds(Collections.singletonList(currentUserId));
        Map<Long, String> userAvatarPath = UserServiceHelper.getUserAvatarPath(Collections.singletonList(currentUserId), true);
        Map<String, Object> info = UserServiceHelper.getUserInfoByID(currentUserId);
        Object number = info.get("number");
        Object phone = info.get("phone");
        Long companyId = company.get(currentUserId);
        String imgURL = userAvatarPath.get(currentUserId);
        this.getModel().setValue("c926_dept", orgId);
        this.getModel().setValue("c926_picturefield", imgURL);
        this.getModel().setValue("c926_applyuser", currentUserId);
        this.getModel().setValue("c926_company", companyId);
        this.getModel().setValue("c926_userno", number);
        this.getModel().setValue("c926_phone", phone);
    }

}